/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplepushserver.filesend;

import simplepushserver.filesend.TransferProgressBar;
import simplepushserver.filesend.DataTimeUnits;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/**
 *
 * @author Fille
 */
public class StatisticsJPanel extends JPanel implements ActionListener {

    private static final long serialVersionUID = 0;
    private Timer timer = new Timer(1000, this);
    private TransferProgressBar parent;
    private DataTimeUnits enhet;
    private JLabel fNameLb;
    private JLabel pTimeLb;
    private JLabel eTimeLb;
    private JLabel unitSpeedLb;
    private Time passedTime = new Time();
    private Time timeLeft = new Time();
    private int seconds = 0;
    private long oldValue = 0;
    private long maxValue = 0;
    private ArrayList<Integer> deltaList = new ArrayList<Integer>();

    public StatisticsJPanel(TransferProgressBar parent, DataTimeUnits u) {
        this.setBorder(new EmptyBorder(7, 7, 0, 7));
        this.parent = parent;
        this.enhet = u;
        this.maxValue = parent.getMaxValue();

        fNameLb = new JLabel("Filename: ");
        fNameLb.setFont(new java.awt.Font("Calbri", Font.BOLD, 12));
        pTimeLb = new JLabel("Elapsed Time: ");
        pTimeLb.setFont(new java.awt.Font("Calbri", Font.BOLD, 12));
        eTimeLb = new JLabel("Time left: ");
        eTimeLb.setFont(new java.awt.Font("Calbri", Font.BOLD, 12));
        unitSpeedLb = new JLabel("Speed: ");
        unitSpeedLb.setFont(new java.awt.Font("Calbri", Font.BOLD, 12));

        GridBagLayout layout = new GridBagLayout();

        GridBagConstraints rad0 = new GridBagConstraints();
        rad0.gridx = 0;
        rad0.gridy = 0;
        rad0.gridheight = 1;
        rad0.gridwidth = 3;
        rad0.fill = GridBagConstraints.BOTH;

        GridBagConstraints rad1 = new GridBagConstraints();
        rad1.gridx = 0;
        rad1.gridy = 1;
        rad1.gridheight = 1;
        rad1.gridwidth = 3;
        rad1.fill = GridBagConstraints.BOTH;

        GridBagConstraints rad2 = new GridBagConstraints();
        rad2.gridx = 0;
        rad2.gridy = 2;
        rad2.gridheight = 1;
        rad2.gridwidth = 3;
        rad2.fill = GridBagConstraints.BOTH;

        GridBagConstraints rad3 = new GridBagConstraints();
        rad3.gridx = 0;
        rad3.gridy = 3;
        rad3.gridheight = 1;
        rad3.gridwidth = 3;
        rad3.fill = GridBagConstraints.BOTH;

        layout.setConstraints(fNameLb, rad0);
        layout.setConstraints(pTimeLb, rad1);
        layout.setConstraints(eTimeLb, rad2);
        layout.setConstraints(unitSpeedLb, rad3);

        setLayout(layout);

        add(fNameLb);
        add(pTimeLb);
        add(eTimeLb);
        add(unitSpeedLb);
    }

    public void setTimeText(int secs) {
        passedTime.setTime(secs);
        pTimeLb.setText("Elapsed Time: " + passedTime);
    }

    public void setFilename(String name) {
        fNameLb.setText("Filename: " + name);
    }

    public void setSpeed(double delta, DataTimeUnits u) {
        delta = getUpdatedDelta(delta);
        if (u == null) {
            if (delta > 2048 && delta < 1024 * 1024) {
                u = DataTimeUnits.KILOBYTES;
                delta = delta / 1024;
            } else if (delta > 1024 * 1024) {
                u = DataTimeUnits.MEGABYTES;
                delta = (delta / (1024 * 1024));
            } else {
                u = DataTimeUnits.BYTES;
            }
        } else {
            if (u == DataTimeUnits.KILOBYTES) {
                delta = delta / 1024;
            } else if (u == DataTimeUnits.MEGABYTES) {
                delta = (delta / 1024) / 1024;
            }
        }
        unitSpeedLb.setText("Speed: " + String.format("%6.2f", delta) + " " + u);
    }

    public void setTimeLeft(double delta, long currentValue) {
        maxValue = parent.getMaxValue();
        long whatsleft = maxValue - currentValue;
        long secondsleft = (int) Math.round(whatsleft / delta);
        timeLeft.setTime(secondsleft);
        eTimeLb.setText("Time left: " + timeLeft);
    }

    public void actionPerformed(ActionEvent e) {
        long currentValue = parent.getProgress();
        long delta = currentValue - oldValue;
        oldValue = currentValue;
        seconds++;
        setSpeed(delta, enhet);
        setTimeText(seconds);
        setTimeLeft(delta, currentValue);
    }

    public void startTime() {
        timer.start();
    }

    public void stopTime() {
        timer.stop();
    }

    public int getUpdatedDelta(double newDelta) {
        deltaList.add((int) newDelta);
        int allDeltaValues = 0;
        for (int cur : deltaList) {
            allDeltaValues += cur;
        }
        return (int) Math.round((double) allDeltaValues / deltaList.size());
    }
}